** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D2.smcl", replace

use "$project_path/data/4_regdata/regdata.dta", clear

local variables = "appreciation imports_wb imp_g_gdpwb imports_cepii exports_wb exp_g_gdpwb exports_cepii trade_wb trade_g_gdpwb trade_intensity tariff_wb hhi_imports hhi_exports share_tax_trade kofgi kofgi_econ kofgi_trade kofgi_fin kofgi_soc kofgi_interper kofgi_inform kofgi_culture kofgi_pol"
local negative_variables = "tariffs_wgt tariff_wb share_tax_trade hhi_imports hhi_exports"
foreach v in `negative_variables' {
	cap gen mstd_diff_m1_`v' = -std_diff_m1_`v'
}

foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
	}
}

log close


** Building .tex table

//Generate a temporary dta file with variables description. We use an id variable that follows the order of the local `variables'.
preserve
gen description = ""

local i = 1
foreach v in `variables'{
	
	if "`v'" == "appreciation" local description = "Exchange rate appreciation"
	if "`v'" == "imports_wb" local description = "Imports (\% of GDP)"
	if "`v'" == "imp_g_gdpwb" local description = "Imports (\% of GDP)"
	if "`v'" == "imports_cepii" local description = "Imports (\% of GDP)"
	if "`v'" == "exports_wb" local description = "Exports (\% of GDP)"
	if "`v'" == "exp_g_gdpwb" local description = "Exports (\% of GDP)"
	if "`v'" == "exports_cepii" local description = "Exports (\% of GDP)"
	if "`v'" == "trade_wb" local description = "Trade intensity"
	if "`v'" == "trade_g_gdpwb" local description = "Trade intensity"
	if "`v'" == "trade_intensity" local description = "Trade intensity"
	if "`v'" == "tariff_wb" local description = "(Minus) Average tariff rate"
	if "`v'" == "hhi_imports" local description = "(Minus) Imports HHI"
	if "`v'" == "hhi_exports" local description = "(Minus) Exports HHI"
	if "`v'" == "share_tax_trade" local description = "(Minus) Taxes on trade (\% of taxes)"
	if "`v'" == "kofgi" local description = "Overall globalization index"
	if "`v'" == "kofgi_econ" local description = "Economic globalization index"
	if "`v'" == "kofgi_trade" local description = "Trade globalization component"
	if "`v'" == "kofgi_fin" local description = "Financial globalization component"
	if "`v'" == "kofgi_soc" local description = "Social globalization index"
	if "`v'" == "kofgi_interper" local description = "Interpersonal globalization component"
	if "`v'" == "kofgi_inform" local description = "Informational globalization component"
	if "`v'" == "kofgi_culture" local description = "Cultural globalization component"
	if "`v'" == "kofgi_pol" local description = "Political globalization index"
	
	qui replace description = "`description'" in `i'
	
	local ++i
}
keep description 
drop if description == ""
gen id = _n
tempfile description_dta
save `description_dta'
restore

//Generate a temporary dta file with source variable
preserve
gen source = ""
local i = 1
foreach v in `variables' {
	
	replace source = "``v'[note1]'" in `i'
	local ++i
	
}	
keep source
drop if source == ""
gen id = _n
replace source = substr(source, 9,.)
tempfile source_dta
save `source_dta'
restore

//Prepare matrix of coefficients and other statistics from the regression
local rows : word count `variables'
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)


local i = 1
foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i
		
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i

	}
}

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

gen outcome = ""

gen id = _n
order id outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}

replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"

//Attach source variable and description
merge 1:1 id using `source_dta', assert(match) nogen
merge 1:1 id using `description_dta', assert(match) nogen

replace outcome = description

drop id description


label var outcome ""
label var estimates "Est."
label var std_err "SE"
label var pval "p-val"
label var N "N"
label var source "Source"

//Prepare texfile

local title "Effects on additional economic performance variables, continued"
local label_key "tab:intecon_variables_baseline"
local output "$project_path/output/appendix_tables/Appendix_Table_D2.tex"
local footnote = "\emph{Notes}: This table reports RD estimates corresponding to equation (\ref{eq:baseline_rdd}) for measures of economic performance, expressed in standard deviation terms, continuing Appendix Table \ref{tab:econ_variables_baseline}"

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{6.8cm} p{1.7cm} p{1.5cm} p{1.5cm} p{1.3cm} p{3.2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 
